/**
 * @(#)PDMediaPlayCommon.java 1.0 2013/9/4
 * Copyright 2013 PD, Inc. All rights reserved.
 */
package cn.broadin.app.hotel.stb.impl.common;

import java.io.IOException;

import cn.broadin.app.hotel.startWebapp.StartWebView;
import cn.broadin.app.hotel.stb.IPDMediaPlay;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Message;
import android.util.Log;
import android.webkit.JavascriptInterface;

/**
 * PDMediaPlayCommon 音频播放通用
 * @author fenghui
 *
 */
public class PDMediaPlayCommon implements IPDMediaPlay,MediaPlayer.OnPreparedListener {

	private static final String TAG = "PDMediaPlayCommon";
	private Context context = null;
	private MediaPlayer localMediaPlayer = null;
	private String audioSource = "";
	private boolean isMuteState = false;
	
	public PDMediaPlayCommon(Context ctx) {
		this.context = ctx;
	}

	@Override
	@JavascriptInterface
	public int createPlayerInstance(String audioType, int whichLayer) {
		if (localMediaPlayer == null) {
			localMediaPlayer = new MediaPlayer();
		}
		localMediaPlayer.setOnPreparedListener(this);
		//音频播放完毕事件
		localMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
			// @Override
			public void onCompletion(MediaPlayer arg0) {
				Log.i(TAG, "audio played Completion!");
				Message msg = StartWebView.mHandler.obtainMessage();
				msg.what = 0;
				StartWebView.mHandler.sendMessage(msg);
			}
		});
		return 1;
	}

	@Override
	@JavascriptInterface
	public void dataSource(String mediaSource) {
		this.audioSource = mediaSource;
	}

	@Override
	@JavascriptInterface
	public int bindPlayerInstance(int instanceId) {
		return 1;
	}

	@Override
	@JavascriptInterface
	public int unBindPlayerInstance() {
		return 1;
	}

	@Override
	@JavascriptInterface
	public int releasePlayerInstance() {
		if(localMediaPlayer != null) {
			localMediaPlayer.release();
			localMediaPlayer = null;
		}
		return 0;
	}

	@Override
	@JavascriptInterface
	public int getMute() {
		return isMuteState ? 1 : 0;
	}

	@Override
	@JavascriptInterface
	public int play() {
		try {
			localMediaPlayer.start();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		}
		return 1;
	}

	@Override
	@JavascriptInterface
	public int pause(int mode) {
		if(localMediaPlayer != null) {
			localMediaPlayer.pause();
		}
		return 1;
	}

	@Override
	@JavascriptInterface
	public int refreshSource() {
		if(localMediaPlayer == null) {
			return 0;
		}
		try {
			localMediaPlayer.reset();
			localMediaPlayer.setDataSource(audioSource);
			localMediaPlayer.prepareAsync();
		} catch (IllegalArgumentException e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		} catch (SecurityException e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		} catch (IllegalStateException e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			Log.e(TAG, e.getMessage());
			e.printStackTrace();
		}
		return 1;
	}

	@Override
	@JavascriptInterface
	public String currentPoint() {
		if(localMediaPlayer != null && localMediaPlayer.isPlaying()) {
			int playedPoint = localMediaPlayer.getCurrentPosition();
			return ((int)Math.floor(playedPoint / 1000)) + "";
		}
		return "1";
	}

	@Override
	@JavascriptInterface
	public String getMediaDuration() {
		if(localMediaPlayer != null) {
			return localMediaPlayer.getDuration() + "";
		}
		return null;
	}

	@Override
	@JavascriptInterface
	public void audioMute() {
		isMuteState = true;
		AudioManager audioMa = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		audioMa.setStreamVolume(AudioManager.STREAM_MUSIC, 0, AudioManager.ADJUST_SAME);
	}

	@Override
	@JavascriptInterface
	public void audioUnmute() {
		isMuteState = false;
		AudioManager audioMa = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
		audioMa.setStreamVolume(AudioManager.STREAM_MUSIC, PDDataAccessCommon.getCurrentVol(), AudioManager.ADJUST_SAME);
	}

	@Override
	@JavascriptInterface
	public void onPrepared(MediaPlayer mp) {
		mp.start();
	}	
}